package com.example.dell.zyfypt_125yy.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.Toast;

import com.example.dell.zyfypt_125yy.R;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener{
    private Button dl;
    private Button zc;
    private EditText name,pass;
    private Switch sw;
    private String username="",password="";
    private SharedPreferences sp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
     init();
        sp=getSharedPreferences("login",MODE_PRIVATE);
        readSP();
    }

    private void readSP() {
        String username=sp.getString("username",null);
        String password=sp.getString("password",null);
        Boolean b=sp.getBoolean("remember",false);
        if(b)
        {
           name.setText(username);
            pass.setText(password);
            sw.setChecked(b);
        }
    }

    private void init() {
        name=(EditText)findViewById(R.id.editTextname);
        pass=(EditText)findViewById(R.id.editTextpass);
        dl=(Button)findViewById(R.id.buttondl);
        zc=(Button)findViewById(R.id.buttonzc);
        sw=(Switch)findViewById(R.id.switch1);


        name.setOnClickListener(this);
        pass.setOnClickListener(this);
        dl.setOnClickListener(this);
        zc.setOnClickListener(this);
        sw.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.buttondl:
                username=name.getText().toString();
                password=pass.getText().toString();
                if(username.equals("14111800125")&&password.equals("11"))
                {
                    Toast.makeText(LoginActivity.this,"登录成功",Toast.LENGTH_SHORT).show();
                    if(sw.isChecked())
                    {
                        saveSP();

                    }

                }
                Intent intent=new Intent(LoginActivity.this,MainActivity.class);
                startActivity(intent);
                break;
            case R.id.buttonzc:
                Intent intent1=new Intent(LoginActivity.this,RegisterActivity.class);
                startActivity(intent1);
                break;

        }
    }

    private void saveSP() {
        SharedPreferences.Editor editor=sp.edit();
        editor.putString("username",username);
        editor.putString("password",password);
        editor.putBoolean("remember",sw.isChecked());
        editor.commit();//提交保存
    }
}
